#
# Copyright (C) [2024] Xingyun Integrated Circuit, Inc.
#
# GreenCode was a private technology asset of Xingyun Integrated Circuit， Inc （Confidential）
# Author: Shawn.Tan
# Date: 2025.10.28
#
# History: Initial Version 2025.10.28
#
#
from PageFaultSequence import PageFaultResolutionType
from PageFaultSequence import PageFaultSequence
from gpgpu.EnvGPGPU import EnvGPGPU
from gpgpu.GenThreadGPGPU import GenThreadGPGPU
from gpgpu.ModifierUtils import AccessFaultModifier


#  This test verifies recover from a Access Exception on a load or store.
class MainSequence(PageFaultSequence):
    def __init__(self, gen_thread, name=None):
        super().__init__(gen_thread, name)

        # just a couple of load/stores...

        self._mInstrList = ["LD##CALM", "SD##CALM"]
        if self.getGlobalState("AppRegisterWidth") == 32:
            self._mInstrList = ["LW##CALM", "SW##CALM"]

        # for gpgpu, access exception - load: 5, store: 7
        self._mExceptionCodes = (
            5,
            7,
        )

        # for now, gpgpu does not support exception sub-codes
        self._mExceptionSubCodes = {}

    def createPageFaultModifier(self):
        return AccessFaultModifier(self.genThread)

    def getInstructionList(self):
        return self.mInstrList

    def getExceptionCodes(self):
        return self._mExceptionCodes

    def getPageFaultResolutionType(self, aFaultLevels, aFastHandlers):
        resolution_type = PageFaultResolutionType.SKIP_INSTRUCTION
        if any([x in aFaultLevels for x in (0, 1, 2, 3)]) and (not aFastHandlers):
            resolution_type = PageFaultResolutionType.RE_EXECUTE_INSTRUCTION

        return resolution_type


MainSequenceClass = MainSequence
GenThreadClass = GenThreadGPGPU
EnvClass = EnvGPGPU
